package cn.com.yahoo.ma.tingqiang.alarm.report;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.com.yahoo.ma.tingqiang.alarm.Alarm;
import cn.com.yahoo.ma.tingqiang.alarm.AlarmList;
import cn.com.yahoo.ma.tingqiang.alarm.util.Constant;

public class AlarmReport {
	AlarmList alarmList;

	public AlarmReport(final AlarmList alarmListParam) {
		this.alarmList = alarmListParam;
	}

	public final String getReport() {
		List<Alarm> list = alarmList.getAlarms();
		StringBuilder strbuilder = new StringBuilder();
		strbuilder.append(ReportConstant.REPORT_TITLE);
		strbuilder.append(Constant.NEWLINE);
		short index = 0;
		for (Alarm alarm : list) {
			index++;
			strbuilder.append(index);
			strbuilder.append(".\t");
			strbuilder.append(formattedTime(alarm));
			strbuilder.append(ReportConstant.FIELD_DELIMITER);
			strbuilder.append(dividedInfo(alarm.getInfo()));
			strbuilder.append(Constant.NEWLINE);

		}
		strbuilder.append(ReportConstant.REPORT_END);
		strbuilder.append(Constant.NEWLINE);

		return strbuilder.toString();
	}

	private String dividedInfo(String info) {
		StringBuilder strbuilder = new StringBuilder();
		for (String part : info.split(ReportConstant.PART_DELIMITER)) {
			strbuilder.append(part);
			strbuilder.append("\t");
		}
		return strbuilder.toString();
	}

	private String formattedTime(final Alarm alarm) {
		long alarmTimeValue = alarm.getTimeValue();

		Date date = new Date();
		date.setTime(alarmTimeValue);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String formattedTime = formatter.format(date);

		return formattedTime;
	}

}
